class UserManager {
    constructor(app) {
        this.app = app;
    }
    
    async getUserById(uid, options) {
        const UserProfile = this.app.db.UserProfile;
        const Passport = this.app.db.Passport;
        const Role = this.app.db.Role;
        const Corp = this.app.db.Corp;
        if (!(!!options && options.force)) {
            let userjson = await this.app.redis.client.getAsync(`shop_uid_${uid}`);
            if (!!userjson) {
                try {
                    let _user = JSON.parse(userjson);
                    return _user
                } catch (e) { }
            }
        }
        let dbuser = await this.app.db.User.findOne({ where: { id: uid },include: [UserProfile, Passport, Role, Corp]}) //include: [UserProfile, Passport, Role, Corp]
        dbuser = JSON.parse(JSON.stringify(dbuser));
        if (dbuser !== null) {
            this.app.redis.client.set(`shop_uid_${uid}`, JSON.stringify(dbuser))
        }
        return dbuser;
    }
    async getUserByUserName(username) {
        let user = await this.app.db.User.findOne({ where: { username: username } })
        return user.toJSON();
    }
    async getUserByUserMobile(mobile) {        
        let users = await this.app.db.User.findAll({ where: { mobile: mobile }, raw: false })
        return users;
    }
    async saveUser(data) {
        let user = await this.app.db.User.findOne({ where: { id: data.id } })
        Object.assign(user, data);
        user.save();
        return user;
    }
    
}
module.exports = app => {
    return new UserManager(app)
};
